import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> ret = new ArrayList<Integer>();
        char[] s1 = s.toCharArray();
        char[] p1 = p.toCharArray();

        int[] hash1 = new int[26];
        //存p1
        for(char ch : p1){
            hash1[ch - 'a']++;
        }
        //存s1
        int[] hash2 = new int[26];
        for(int left = 0,right = 0,count = 0;right < s1.length;right++){
            //入窗口
            hash2[s1[right] - 'a']++;
            //判读是否为有效值
            if(hash1[s1[right] - 'a'] >= hash2[s1[right] - 'a']){
                count++;
            }
            if(right-left + 1 > p1.length){
                if(hash1[s1[left]-'a'] >= hash2[s1[left] - 'a']){
                    count--;
                }
                //出窗口
                hash2[s1[left] - 'a']--;
                left++;
            }
            if(count == p1.length){
                ret.add(left);
            }
        }
        return ret;
    }
}